package com.example.chiro;

import java.util.ArrayList;

import com.actionbarsherlock.internal.view.View_HasStateListenerSupport;
import com.example.chiro.R.id;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;

public class Adaptor_cavite_item extends ArrayAdapter<Donnees_cavite> {

	//Context context;
	int layoutResourceId;
	Holder holder;
	ArrayList<Donnees_cavite> liste;
	private LayoutInflater mInflater;
	private Context context;
	

	// TODO Auto-generated constructor stub
	public Adaptor_cavite_item(Context context, int layoutResourceId,
			ArrayList<Donnees_cavite> data) {
		super(context, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		mInflater = LayoutInflater.from(context);
		this.liste = data ;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

			
			

		View view = null;
		if (convertView == null){
			LayoutInflater inflate = ((Activity) context).getLayoutInflater();
			view = inflate.inflate(R.layout.slide_item_cavite, null);
			final Holder ViewHolder = new Holder();
			
			
			//ViewHolder.num_cavite = (TextView)view.findViewById(R.id.num_cavite);
			//ViewHolder.num_cavite.setText("Cavité : "+Slide_cavite.num_cavite);
			//Donnees_cavite element = (Donnees_cavite)ViewHolder.num_cavite.getTag();
			//element.setNum_cavite(position+1);
			//liste.get(position).setNum_cavite(Slide_cavite.num_cavite);
			
			
			
			ViewHolder.type = (Spinner) view.findViewById(R.id.slide_cavite_type);
			ArrayAdapter<CharSequence> adapter_type = ArrayAdapter.createFromResource(mInflater.getContext(), R.array.cavite_type,android.R.layout.simple_spinner_item);
			adapter_type.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			ViewHolder.type.setAdapter(adapter_type);
			
			
			ViewHolder.type.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					//Log.i("test spinner", position+" : "+arg0.getItemAtPosition(arg2).toString());
					Donnees_cavite element = (Donnees_cavite)ViewHolder.type.getTag();
					element.setSpin_type_selected(arg0.getSelectedItemPosition());
					if (arg2 != 0)
						element.setT_type(String_operation.op_string(arg0.getItemAtPosition(arg2).toString()));
					else 
						element.setT_type("NC");
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
					
				}
			});
			
			ViewHolder.fibroscope = (EditText)view.findViewById(R.id.slide_cavite_fibroscope);
			ViewHolder.fibroscope.addTextChangedListener(new TextWatcher() {
				
				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {
				}
				
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count,
						int after) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub
					Donnees_cavite element = (Donnees_cavite)ViewHolder.fibroscope.getTag();
					element.setT_fibroscope(s.toString());
					//ViewHolder.fibroscope.clearFocus();
					Log.i("test text", position+" : "+s.toString());
				}
			});
			
			
			ViewHolder.excrement = (RadioGroup)view.findViewById(R.id.slide_cavite_excrement);
			ViewHolder.excrement.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(RadioGroup arg0, int arg1) {
					Donnees_cavite element = (Donnees_cavite)ViewHolder.excrement.getTag();
					element.setRb_excrement_checked(arg0.getCheckedRadioButtonId());
					
					for(int i=0;i<arg0.getChildCount();i++){
						RadioButton btn = (RadioButton)arg0.getChildAt(i);
						if(btn.getId() == arg1){
							element.setT_coulure_excrement((String) btn.getText());
							return;
						}
					}
					
				}
			});
			
			
			ViewHolder.guano = (RadioGroup)view.findViewById(R.id.slide_cavite_guano);
			ViewHolder.guano.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(RadioGroup arg0, int arg1) {
					Donnees_cavite element = (Donnees_cavite)ViewHolder.guano.getTag();
					element.setRb_guano_checked(arg0.getCheckedRadioButtonId());
					
					for(int i=0;i<arg0.getChildCount();i++){
						RadioButton btn = (RadioButton)arg0.getChildAt(i);
						if(btn.getId() == arg1){
							element.setT_guano((String) btn.getText());
							return;
						}
					}
					
				}
			});
			
			
			ViewHolder.position = (RadioGroup)view.findViewById(R.id.slide_cavite_position);
			ViewHolder.position.setOnCheckedChangeListener(new OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(RadioGroup arg0, int arg1) {
					Donnees_cavite element = (Donnees_cavite)ViewHolder.position.getTag();
					element.setRb_position_checked(arg0.getCheckedRadioButtonId());
					
					for(int i=0;i<arg0.getChildCount();i++){
						RadioButton btn = (RadioButton)arg0.getChildAt(i);
						if(btn.getId() == arg1){
							element.setT_position((String) btn.getText());
							return;
						}
					}
					
				}
			});
			
			view.setTag(ViewHolder);
			//ViewHolder.num_cavite.setTag(liste.get(position));
			ViewHolder.type.setTag(liste.get(position));
			ViewHolder.fibroscope.setTag(liste.get(position));
			ViewHolder.excrement.setTag(liste.get(position));
			ViewHolder.guano.setTag(liste.get(position));
			ViewHolder.position.setTag(liste.get(position));
			
			
		}else{
			//holder = (Holder) row.getTag();
			view = convertView;
			//((Holder)view.getTag()).num_cavite.setTag(liste.get(position));
			((Holder)view.getTag()).type.setTag(liste.get(position));
			((Holder)view.getTag()).fibroscope.setTag(liste.get(position));
			((Holder)view.getTag()).excrement.setTag(liste.get(position));
			((Holder)view.getTag()).guano.setTag(liste.get(position));
			((Holder)view.getTag()).position.setTag(liste.get(position));
			
		}
		
		Holder holder = (Holder)view.getTag();
		//holder.num_cavite.setText("Cavité : "+liste.get(position).getNum_cavite());
		holder.type.setSelection(liste.get(position).getSpin_type_selected());
		holder.fibroscope.setText(liste.get(position).getT_fibroscope());
		holder.excrement.check(liste.get(position).getRb_excrement_checked());
		holder.guano.check(liste.get(position).getRb_guano_checked());
		holder.position.check(liste.get(position).getRb_position_checked());
		//holder.t_coulure_excrement = liste.get(position).getCoulure_excrement();
		
		//holder.t_type = liste.get(position).getType();
		TextView numero = (TextView)view.findViewById(R.id.num_cavite);
		numero.setText("Cavité : "+liste.get(position).getNum_cavite()+"/100");
		
		/*
		if (liste.get(position).getNum_cavite()%2 == 0){
			view.setBackgroundColor(Color.LTGRAY);
		}
		*/
		
		// GESTION PHOTO , MAIS MIT DE COTE POUR L'INSTANT
		/*
		holder.photo = (ImageButton) view.findViewById(R.id.slide_cavite_button_photo);
		holder.photo.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                //getActivity().startActivityForResult(intent, 1111);
                Toast.makeText(context, String.valueOf(liste.get(position).getNum_cavite()+100), Toast.LENGTH_SHORT).show();
                ((Activity)context).startActivityForResult(intent,liste.get(position).getNum_cavite()+100);
            }
        });
		*/
		
		
		return view;
		
	}

	static class Holder {
		ImageButton photo;
		Spinner type;
		EditText fibroscope;
		RadioGroup excrement, guano, position;
		//TextView num_cavite;
		//String t_coulure_excrement = "", t_type;
		
		String photo_path;

		public String toString() {
			return "Holder [type=" + type.getSelectedItemPosition()
					+ ", fibroscope=" + fibroscope.getText() + ", excrement="
					+ excrement.getCheckedRadioButtonId() + ", guano="
					+ guano.getCheckedRadioButtonId() + ", position="
					+ position.getCheckedRadioButtonId() + "]";
		}
	}

}
